﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;

namespace Actor_Editor
{
    public partial class Form1 : Form
    {
        private OpenFileDialog openFileDialog;
        private SaveFileDialog saveFileDialog;
        private bool saved;
        private Image imageFile;
        private Image[] frame;
        private String currFile;
        private Int32 currAngle;
        private float[] xfactor, yfactor;
        private int circleCount, lineCount;
        
        public Form1()
        {
            InitializeComponent();

            openFileDialog = new OpenFileDialog();
            openFileDialog.RestoreDirectory = false;
            openFileDialog.InitialDirectory = Application.StartupPath;

            saveFileDialog = new SaveFileDialog();
            saveFileDialog.RestoreDirectory = false;
            saveFileDialog.InitialDirectory = Application.StartupPath;

            xfactor = new float[7];
            yfactor = new float[7];

            xfactor[0] = 1;
            xfactor[1] = (float) Math.Sqrt(3) / 2;
            xfactor[2] = 0.5f;
            xfactor[3] = 0;
            xfactor[4] = - xfactor[2];
            xfactor[5] = - xfactor[1];
            xfactor[6] = - xfactor[0];

            yfactor[0] = xfactor[3];
            yfactor[1] = xfactor[2];
            yfactor[2] = xfactor[1];
            yfactor[3] = xfactor[0];
            yfactor[4] = - xfactor[5];
            yfactor[5] = - xfactor[4];
            yfactor[6] = - xfactor[3];

            circleCount = 0;
            lineCount = 0;
        }

        private void comboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            tabControl.SelectedIndex = comboBox.SelectedIndex;
            valueChanged(sender, e);
        }

        private void tabControl_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBox.SelectedIndex = tabControl.SelectedIndex;
        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            lblSprite.Left = this.Width - 231;
            txtSprite.Left = this.Width - 188;
            button.Left = this.Width - 85;
            lblType.Left = this.Width - 228;
            comboBox.Left = this.Width - 188;
            tabControl.Left = this.Width - 231;

            pictureBox.Width = this.Width - 249;
            pictureBox.Height = pictureBox.Width;

            this.Height = pictureBox.Height + 74;
        }

        private void newActorToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!saved)
            {
                DialogResult myResult = MessageBox.Show("Save changes to current actor?", "Actor Editor", MessageBoxButtons.YesNoCancel);

                if (myResult == DialogResult.Yes)
                {
                    saveToolStripMenuItem_Click(this, null);
                }
                else if (myResult == DialogResult.Cancel)
                {
                    return;
                }
            }

            clearAll();

            saved = true;
            currFile = null;
        }

        private void clearAll()
        {
            this.Text = "Actor Editor";

            txtSprite.Text = "";
            
            foreach (TabPage tab in tabControl.TabPages)
                foreach (Control someControl in tab.Controls)
                    if (someControl.Name.Substring(0, 3) == "txt")
                        ((TextBox)someControl).Text = "";

            comboBoxCircle.Items.Clear();
            txtOCX.Enabled = false;
            txtOCY.Enabled = false;
            txtOCR.Enabled = false;

            comboBoxLine.Items.Clear();
            txtOLX1.Enabled = false;
            txtOLY1.Enabled = false;
            txtOLX2.Enabled = false;
            txtOLY2.Enabled = false;
            
            imageFile = null;
            frame = null;

            updateImage();
        }

        private void button_Click(object sender, EventArgs e)
        {
            openFileDialog.Filter = "Sprite files (*.spr)|*.spr|All files (*.*)|*.*";
            openFileDialog.FilterIndex = 1;

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                loadSprite(openFileDialog.FileName);
            }

            openFileDialog.Reset();
        }

        private void loadSprite(string file)
        {
            StreamReader myStream;

 	        if ((myStream = new StreamReader(file)) != null)
            {
                txtSprite.Text = file;

                try
                {
                    String line;

                    line = myStream.ReadLine();
                    imageFile = new Bitmap(Path.GetDirectoryName(file).Replace("Sprites", "") + line);

                    line = myStream.ReadLine(); // Absorb States
                    line = myStream.ReadLine(); // Absorb Name

                    int frames = Convert.ToInt32(myStream.ReadLine());

                    while (frames-- > 0)
                        line = myStream.ReadLine(); // Absorb durations

                    int bottom, height, width;

                    bottom = Convert.ToInt32(myStream.ReadLine()); // Read Bottom
                    height = Convert.ToInt32(myStream.ReadLine()); // Read Height
                    width = Convert.ToInt32(myStream.ReadLine()); // Read Width

                    frame = new Image[12];

                    for (int angle = 0; angle < 7; angle++)
                    {
                        Rectangle crop = new Rectangle(angle * width, imageFile.Height - (bottom + height), width, height);
                        frame[angle] = new Bitmap(imageFile).Clone(crop, System.Drawing.Imaging.PixelFormat.DontCare);
                    }

                    for (int angle = 7; angle < 12; angle++)
                    {
                        frame[angle] = new Bitmap(frame[12 - angle]);
                        frame[angle].RotateFlip(RotateFlipType.RotateNoneFlipX);
                    }

                    currAngle = 0;

                    updateImage();
                    autoResizeToolStripMenuItem_Click(this, null);

                    myStream.Close();
                    saved = true;
                }
                catch (Exception e)
                {
                    MessageBox.Show("Unable to open " + file + ". Error: " + e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void updateImage()
        {
            pictureBox.Image = createImage();
        }

        private Image createImage()
        {
            try
            {
                int diameter = 0, collision = 0, fear = 0, pounce = 0;

                try
                {
                    if (comboBox.SelectedIndex == 0)
                        collision = Convert.ToInt32(txtWCol.Text);
                    else if (comboBox.SelectedIndex == 1)
                        collision = Convert.ToInt32(txtDCol.Text);
                    else if (comboBox.SelectedIndex == 2)
                        collision = Convert.ToInt32(txtSCol.Text);
                    else 
                        collision = 0;
                }
                catch (Exception)
                {
                    collision = 0;
                }

                try
                {
                    if (comboBox.SelectedIndex == 0)
                        fear = Convert.ToInt32(txtWFear.Text);
                    else if (comboBox.SelectedIndex == 1)
                        fear = Convert.ToInt32(txtDFear.Text);
                    else if (comboBox.SelectedIndex == 2)
                        fear = Convert.ToInt32(txtSFear.Text);
                    else
                        fear = 0;
                }
                catch (Exception)
                {
                    fear = 0;
                }

                try
                {
                    if (comboBox.SelectedIndex == 0)
                        pounce = Convert.ToInt32(txtWPounce.Text);
                    else if (comboBox.SelectedIndex == 1)
                        pounce = Convert.ToInt32(txtDPounce.Text);
                    else
                        pounce = 0;
                }
                catch (Exception)
                {
                    pounce = 0;
                }

                diameter = Math.Max(collision, fear) * 2;
                diameter = Math.Max(diameter, pounce * 2);
                diameter = Math.Max(diameter, frame[0].Height);
                diameter = Math.Max(diameter, frame[0].Width);

                if (comboBox.SelectedIndex == 3)
                {
                    try
                    {
                        int center_x = Convert.ToInt32(txtOX.Text);
                        int center_y = Convert.ToInt32(txtOY.Text);

                        foreach (Circle circle in comboBoxCircle.Items)
                            diameter = Math.Max(diameter, circle.m + circle.r + Math.Max(center_x, center_y));

                        foreach (Line line in comboBoxLine.Items)
                            diameter = Math.Max(diameter, line.m + Math.Max(center_x, center_y));
                    }
                    catch (Exception)
                    {
                    }
                }

                Image canvas = new Bitmap(diameter + 1, diameter + 1);
                Graphics overlay = Graphics.FromImage(canvas);
                Brush myBrush;

                myBrush = new SolidBrush(Color.FromArgb(127, Color.Green));
                overlay.FillEllipse(myBrush, diameter / 2 - fear, diameter / 2 - fear, fear * 2, fear * 2);

                myBrush = new SolidBrush(Color.FromArgb(127, Color.Blue));
                overlay.FillEllipse(myBrush, diameter / 2 - collision, diameter / 2 - collision, collision * 2, collision * 2);

                overlay.DrawEllipse(new Pen(Color.Green), diameter / 2 - fear, diameter / 2 - fear, fear * 2, fear * 2);
                overlay.DrawEllipse(new Pen(Color.Blue), diameter / 2 - collision, diameter / 2 - collision, collision * 2, collision * 2);
                overlay.DrawEllipse(new Pen(Color.Red), diameter / 2 - pounce, diameter / 2 - pounce, pounce * 2, pounce * 2);

                overlay.DrawImage(frame[currAngle], diameter / 2 - frame[0].Width / 2, diameter / 2 - frame[0].Height / 2);
                
                if (comboBox.SelectedIndex == 3)
                {
                    try
                    {
                        int center_x = Convert.ToInt32(txtOX.Text);
                        int center_y = Convert.ToInt32(txtOY.Text);

                        foreach (Circle circle in comboBoxCircle.Items)
                        {
                            myBrush = new SolidBrush(Color.FromArgb(127, Color.Blue));
                            overlay.FillEllipse(myBrush, frame[0].Width / 2 + center_x + circle.x[currAngle] - circle.r, frame[0].Height - (frame[0].Height / 2 + center_y) - circle.y[currAngle] - circle.r - 1, circle.r * 2, circle.r * 2);
                            overlay.DrawEllipse(new Pen(Color.Blue), frame[0].Width / 2 + center_x + circle.x[currAngle] - circle.r, frame[0].Height - (frame[0].Height / 2 + center_y) - circle.y[currAngle] - circle.r - 1, circle.r * 2, circle.r * 2);
                        }

                        foreach (Line line in comboBoxLine.Items)
                        {
                            overlay.DrawLine(new Pen(Color.Blue, 2), frame[0].Width / 2 + center_x + line.x1[currAngle], frame[0].Height - (frame[0].Height / 2 + center_y) - line.y1[currAngle] + 1, frame[0].Width / 2 + center_x + line.x2[currAngle], frame[0].Height - (frame[0].Height / 2 + center_y) - line.y2[currAngle] + 1);
                        }
                    }
                    catch (Exception)
                    {
                    }

                }

                return canvas;
            }
            catch
            {
                return null;
            }
        }

        private void autoResizeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            pictureBox.Width = pictureBox.Image.Width;
            pictureBox.Height = pictureBox.Image.Height;

            this.Width = pictureBox.Width + 249;
            this.Height = pictureBox.Height + 74;

            Form1_Resize(sender, e);
        }

        private void aboutActorEditorToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Actor Editor v1.0 is designed to support the development of LoneDawg, LoneWolf and LoneWolf-R. © 2010 Luv & Caleb.", "About Actor Editor", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void valueChanged(object sender, EventArgs e)
        {
            saved = false;
            updateImage();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            openFile(Application.StartupPath + @"\Actors\dog.act");
        }

        private void openFile(string file)
        {
            StreamReader myStream;

            try
            {

                if ((myStream = new StreamReader(file)) != null)
                {
                    try
                    {
                        clearAll();

                        String line;

                        line = myStream.ReadLine();
                        txtSprite.Text = Path.GetDirectoryName(file).Replace("Actors", "") + line;
                        loadSprite(txtSprite.Text);

                        Hashtable types = new Hashtable();
                        types.Add("Wolf", 0);
                        types.Add("Dog", 1);
                        types.Add("Sheep", 2);
                        types.Add("Obstacle", 3);

                        comboBox.SelectedIndex = (int) types[myStream.ReadLine()];

                        if (comboBox.SelectedIndex == 0)
                        {
                            txtWName.Text = myStream.ReadLine();
                            txtWCol.Text = myStream.ReadLine();
                            txtWSpeed.Text = myStream.ReadLine();
                            txtWFear.Text = myStream.ReadLine();
                            txtWPounce.Text = myStream.ReadLine();
                            txtWHP.Text = myStream.ReadLine();
                            txtWAtt.Text = myStream.ReadLine();
                        }
                        else if (comboBox.SelectedIndex == 1)
                        {
                            txtDName.Text = myStream.ReadLine();
                            txtDCol.Text = myStream.ReadLine();
                            txtDSpeed.Text = myStream.ReadLine();
                            txtDFear.Text = myStream.ReadLine();
                            txtDPounce.Text = myStream.ReadLine();
                            txtDAtt.Text = myStream.ReadLine();
                            txtDStam.Text = myStream.ReadLine();
                            txtDRegen.Text = myStream.ReadLine();
                        }
                        else if (comboBox.SelectedIndex == 2)
                        {
                            txtSName.Text = myStream.ReadLine();
                            txtSCol.Text = myStream.ReadLine();
                            txtSSpeed.Text = myStream.ReadLine();
                            txtSFear.Text = myStream.ReadLine();
                            txtSRest.Text = myStream.ReadLine();
                            txtSHP.Text = myStream.ReadLine();
                        }
                        else if (comboBox.SelectedIndex == 3)
                        {
                            txtOName.Text = myStream.ReadLine();
                            txtOX.Text = myStream.ReadLine();
                            txtOY.Text = myStream.ReadLine();

                            circleCount = Convert.ToInt32(myStream.ReadLine());

                            for (int i = 0; i < circleCount; i++)
                                comboBoxCircle.Items.Add(new Circle(i + 1, Convert.ToInt32(myStream.ReadLine()), Convert.ToInt32(myStream.ReadLine()), Convert.ToInt32(myStream.ReadLine())));

                            if (circleCount > 0)
                            {
                                comboBoxCircle.SelectedIndex = circleCount - 1;
                                txtOCX.Enabled = true;
                                txtOCY.Enabled = true;
                                txtOCR.Enabled = true;
                            }

                            lineCount = Convert.ToInt32(myStream.ReadLine());

                            for (int i = 0; i < lineCount; i++)
                                comboBoxLine.Items.Add(new Line(i + 1, Convert.ToInt32(myStream.ReadLine()), Convert.ToInt32(myStream.ReadLine()), Convert.ToInt32(myStream.ReadLine()), Convert.ToInt32(myStream.ReadLine())));

                            if (lineCount > 0)
                            {
                                comboBoxLine.SelectedIndex = lineCount - 1;
                                txtOLX1.Enabled = true;
                                txtOLY1.Enabled = true;
                                txtOLX2.Enabled = true;
                                txtOLY2.Enabled = true;
                            }
                        }
                        
                        saved = true;
                        currFile = file;
                        autoResizeToolStripMenuItem_Click(this, null);

                        this.Text = "Actor Editor - " + Path.GetFileNameWithoutExtension(currFile) + ".act";
                    }
                    catch (Exception)
                    {
                        clearAll();
                        saved = true;
                        currFile = null;
                        MessageBox.Show("Unable to open " + file + ". Invalid format.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("Unable to open " + file + ". " + e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void saveFile(string file)
        {
            try
            {
                StringBuilder output = new StringBuilder();
                StreamWriter myStream;

                if ((myStream = new StreamWriter(file)) != null)
                {
                    output.AppendLine("Sprites/" + Path.GetFileNameWithoutExtension(txtSprite.Text) + ".spr");
                    output.AppendLine(tabControl.SelectedTab.Text);

                    if (comboBox.SelectedIndex == 0)
                    {
                        output.AppendLine(txtWName.Text.Trim());
                        output.AppendLine(txtWCol.Text.Trim());
                        output.AppendLine(txtWSpeed.Text.Trim());
                        output.AppendLine(txtWFear.Text.Trim());
                        output.AppendLine(txtWPounce.Text.Trim());
                        output.AppendLine(txtWHP.Text.Trim());
                        output.AppendLine(txtWAtt.Text.Trim());
                    }
                    else if (comboBox.SelectedIndex == 1)
                    {
                        output.AppendLine(txtDName.Text.Trim());
                        output.AppendLine(txtDCol.Text.Trim());
                        output.AppendLine(txtDSpeed.Text.Trim());
                        output.AppendLine(txtDFear.Text.Trim());
                        output.AppendLine(txtDPounce.Text.Trim());
                        output.AppendLine(txtDAtt.Text.Trim());
                        output.AppendLine(txtDStam.Text.Trim());
                        output.AppendLine(txtDRegen.Text.Trim());
                    }
                    else if (comboBox.SelectedIndex == 2)
                    {
                        output.AppendLine(txtSName.Text.Trim());
                        output.AppendLine(txtSCol.Text.Trim());
                        output.AppendLine(txtSSpeed.Text.Trim());
                        output.AppendLine(txtSFear.Text.Trim());
                        output.AppendLine(txtSRest.Text.Trim());
                        output.AppendLine(txtSHP.Text.Trim());
                    }
                    else if (comboBox.SelectedIndex == 3)
                    {
                        output.AppendLine(txtOName.Text.Trim());
                        output.AppendLine(txtOX.Text.Trim());
                        output.AppendLine(txtOY.Text.Trim());
                        output.AppendLine(comboBoxCircle.Items.Count.ToString());

                        foreach (Circle circle in comboBoxCircle.Items)
                        {
                            output.AppendLine(circle.x[0].ToString());
                            output.AppendLine(circle.y[0].ToString());
                            output.AppendLine(circle.r.ToString());
                        }

                        output.AppendLine(comboBoxLine.Items.Count.ToString());

                        foreach (Line line in comboBoxLine.Items)
                        {
                            output.AppendLine(line.x1[0].ToString());
                            output.AppendLine(line.y1[0].ToString());
                            output.AppendLine(line.x2[0].ToString());
                            output.AppendLine(line.y2[0].ToString());
                        }
                    }

                    output.Replace("\r", "");
                    myStream.Write(output.ToString());
                    myStream.Close();

                    saved = true;
                    currFile = file;

                    this.Text = "Actor Editor - " + Path.GetFileNameWithoutExtension(currFile) + ".act";
                }
            }
            catch (Exception e)
            {
                
                MessageBox.Show("Unable to save " + file + ". Error: " + e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!saved && currFile != null)
                saveFile(currFile);
            else if (!saved)
                saveAsToolStripMenuItem_Click(sender, e);
        }

        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            saveFileDialog.Filter = "Actor files (*.act)|*.act|All files (*.*)|*.*";
            saveFileDialog.FilterIndex = 1;
            saveFileDialog.FileName = Path.GetFileNameWithoutExtension(txtSprite.Text) + ".act";

            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                saveFile(saveFileDialog.FileName);
            }

            saveFileDialog.Reset();
        }

        private void openActorToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!saved)
            {
                DialogResult myResult = MessageBox.Show("Save changes to current actor?", "Actor Editor", MessageBoxButtons.YesNoCancel);

                if (myResult == DialogResult.Yes)
                {
                    saveToolStripMenuItem_Click(sender, e);
                }
                else if (myResult == DialogResult.Cancel)
                {
                    return;
                }
            }

            openFileDialog.Filter = "Actor files (*.act)|*.act|All files (*.*)|*.*";
            openFileDialog.FilterIndex = 1;

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                openFile(openFileDialog.FileName);
            }

            openFileDialog.Reset();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!saved)
            {
                DialogResult myResult = MessageBox.Show("Save changes to current sprite?", "Sprite Editor", MessageBoxButtons.YesNoCancel);

                if (myResult == DialogResult.Yes)
                {
                    saveToolStripMenuItem_Click(this, null);
                }
                else if (myResult == DialogResult.Cancel)
                {
                    e.Cancel = true;
                }
            }
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            currAngle = (currAngle + 1) % 12;
            updateImage();
        }

        private void addNewCircleToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Circle newItem = new Circle(nextCircle());
            comboBoxCircle.Items.Add(newItem);
            comboBoxCircle.SelectedItem = newItem;
            txtOCX.Enabled = true;
            txtOCY.Enabled = true;
            txtOCR.Enabled = true;
        }

        private int nextCircle()
        {
            return ++circleCount;
        }

        private void addNewLineToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Line newItem = new Line(nextLine());
            comboBoxLine.Items.Add(newItem);
            comboBoxLine.SelectedItem = newItem;
            txtOLX1.Enabled = true;
            txtOLY1.Enabled = true;
            txtOLX2.Enabled = true;
            txtOLY2.Enabled = true;
        }

        private int nextLine()
        {
            return ++lineCount;
        }

        private void deleteSelectedLineToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (comboBoxLine.SelectedItem != null)
            {
                comboBoxLine.Items.Remove(comboBoxLine.SelectedItem);

                if (comboBoxLine.Items.Count > 0)
                    comboBoxLine.SelectedIndex = 0;
                else
                {
                    comboBoxLine.Text = "";
                    txtOLX1.Text = "";
                    txtOLY1.Text = "";
                    txtOLX2.Text = "";
                    txtOLY2.Text = "";
                    txtOLX1.Enabled = false;
                    txtOLY1.Enabled = false;
                    txtOLX2.Enabled = false;
                    txtOLY2.Enabled = false;
                    lineCount = 0;
                }
            }
        }

        private void deleteSelectedCircleToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (comboBoxCircle.SelectedItem != null)
            {
                comboBoxCircle.Items.Remove(comboBoxCircle.SelectedItem);

                if (comboBoxCircle.Items.Count > 0)
                    comboBoxCircle.SelectedIndex = 0;
                else
                {
                    comboBoxCircle.Text = "";
                    txtOCX.Text = "";
                    txtOCY.Text = "";
                    txtOCR.Text = "";
                    txtOCX.Enabled = false;
                    txtOCY.Enabled = false;
                    txtOCR.Enabled = false;
                    circleCount = 0;
                }
            }
        }

        private void comboBoxCircle_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtOCX.Text = ((Circle)comboBoxCircle.SelectedItem).x[0].ToString();
            txtOCY.Text = ((Circle)comboBoxCircle.SelectedItem).y[0].ToString();
            txtOCR.Text = ((Circle)comboBoxCircle.SelectedItem).r.ToString();
        }

        private void txtOCX_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (comboBoxCircle.SelectedItem != null)
                    ((Circle)comboBoxCircle.SelectedItem).setX(Convert.ToInt32(txtOCX.Text));
            }
            catch (Exception)
            {
            }

            valueChanged(sender, e);
        }

        private void txtOCY_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (comboBoxCircle.SelectedItem != null)
                    ((Circle)comboBoxCircle.SelectedItem).setY(Convert.ToInt32(txtOCY.Text));
            }
            catch (Exception)
            {
            }

            valueChanged(sender, e);
        }

        private void txtOCR_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (comboBoxCircle.SelectedItem != null)
                    ((Circle)comboBoxCircle.SelectedItem).r = Convert.ToInt32(txtOCR.Text);
            }
            catch (Exception)
            {
            }

            valueChanged(sender, e);
        }

        private void comboBoxLine_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtOLX1.Text = ((Line)comboBoxLine.SelectedItem).x1[0].ToString();
            txtOLY1.Text = ((Line)comboBoxLine.SelectedItem).y1[0].ToString();
            txtOLX2.Text = ((Line)comboBoxLine.SelectedItem).x2[0].ToString();
            txtOLY2.Text = ((Line)comboBoxLine.SelectedItem).y2[0].ToString();
        }

        private void txtOLX1_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (comboBoxLine.SelectedItem != null)
                    ((Line)comboBoxLine.SelectedItem).setX1(Convert.ToInt32(txtOLX1.Text));
            }
            catch (Exception)
            {
            }

            valueChanged(sender, e);
        }

        private void txtOLY1_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (comboBoxLine.SelectedItem != null)
                    ((Line)comboBoxLine.SelectedItem).setY1(Convert.ToInt32(txtOLY1.Text));
            }
            catch (Exception)
            {
            }

            valueChanged(sender, e);
        }

        private void txtOLX2_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (comboBoxLine.SelectedItem != null)
                    ((Line)comboBoxLine.SelectedItem).setX2(Convert.ToInt32(txtOLX2.Text));
            }
            catch (Exception)
            {
            }

            valueChanged(sender, e);
        }

        private void txtOLY2_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (comboBoxLine.SelectedItem != null)
                    ((Line)comboBoxLine.SelectedItem).setY2(Convert.ToInt32(txtOLY2.Text));
            }
            catch (Exception)
            {
            }

            valueChanged(sender, e);
        }

        private void recalcAll(object sender, EventArgs e)
        {
            foreach (Circle circle in comboBoxCircle.Items)
                circle.recalc();

            foreach (Line line in comboBoxLine.Items)
                line.recalc();

            valueChanged(sender, e);
        }
    }
}
